#include<memory>
#include"Logger.hpp"
#include"InetAddr.hpp"
#include"UdpServer.hpp"
#include"ThreadPool.hpp"
#include"Route.hpp"
#include<functional>

using tast_t = std::function<void()>;

void Chat(int socket_fd, std::string message, InetAddr addr)
{
    LOG(LogLevel::DEBUG) << "socket_fd:" << socket_fd;
    LOG(LogLevel::DEBUG) << "message:" << message;
    LOG(LogLevel::DEBUG) << "addr:" << addr.ToString();
    sendto(socket_fd, message.c_str(), message.size(), 0, addr.Addr(), addr.Len());
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << "port" << std::endl;
        exit(0);
    }
    EnableConsoleLogStrategy();
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<Route> r = std::make_unique<Route>();
    ThreadPool<tast_t>* tp = ThreadPool<tast_t>::GetInstance();
    //std::unique_ptr<UdpServer> udp = std::make_unique<UdpServer>(port, Chat);
    std::unique_ptr<UdpServer> udp = std::make_unique<UdpServer>(port, 
        [&r, &tp](int socket_fd, std::string message, InetAddr addr)
        {
            tast_t task = std::bind(&Route::RouteMessageToAll, r.get(), socket_fd,
                                    message, addr);
            tp->EnQueue(task);
        }
    );
    udp->Init();
    udp->Start();

    return 0;
}